using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace sharpCash.HTTPClasses
{
    /// <summary>
    /// This deals with HTTP posting and Getting.  It might deal with more stuff later.
    /// </summary>
    public class HTTPClass
    {
        static public string HttpPost(string uri, string data, NetworkCredential networkCredentials, CookieContainer cookies, bool OFX)
        {
            System.Net.HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(uri);
            req.Method = "POST";
            if (OFX)
            {
                req.ContentType = "application/x-ofx";
                req.Accept = "application/x-ofx";
            }
            else
            {
                req.ContentType = "application/*";
                req.Accept = "application/*";
            }
            req.UserAgent = "SharpCash 0.4";
            req.KeepAlive = false;
            req.CookieContainer = cookies;
            //req.Credentials = networkCredentials;
            req.AllowAutoRedirect = true;
            System.Net.ServicePointManager.Expect100Continue = false;

            // ENCODE DATA & POST IT
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            // GET RESPONSE
            System.Net.HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            if (resp == null) return null;

            // SAVE COOKIES
            cookies.Add(req.RequestUri, resp.Cookies);

            // READ RESPONSE
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            resp.Close();
            req.KeepAlive = false;
            req.Abort();
            return response;
        }
        static public string HttpGet(string uri, string data, NetworkCredential networkCredentials, CookieContainer cookies)
        {
            System.Net.HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(uri);
            req.Method = "GET";
            req.ContentType = "application/*";
            req.Accept = "application/*";
            req.UserAgent = "SharpCash 0.4";
            req.KeepAlive = false;
            req.CookieContainer = cookies;
            //req.Credentials = networkCredentials;
            req.AllowAutoRedirect = true;
            System.Net.ServicePointManager.Expect100Continue = false;

            // GET RESPONSE
            string website = "";
            try
            {
                System.Net.HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                if (resp == null) return null;

                // SAVE COOKIES
                // READ RESPONSE
                System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
                website = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                resp.Close();
            }
            catch
            {
                req.Abort();
            }
            req.KeepAlive = false;
            req.Abort();
            return website;
        }

    }
    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy()
        {
        }

        public bool CheckValidationResult(ServicePoint sp,
         X509Certificate cert, WebRequest req, int problem)
        {
            return true;
        }
    }

}
